﻿using LambdaToSql.FrameWork;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace LambdaToSql
{
    internal class DbFirst : LambdaToSql.Interface.IDbFirst
    {
        #region 默认模板

        static string mb_basic = @"using System;
                    using System.Collections.Generic;
                    using System.Linq;
                    using System.Runtime.Serialization;
                    using System.Text;
                    using LambdaToSql;
                    using LambdaToSql.FrameWork;
                    using LambdaToSql.Extended;
                    
                    namespace EntityModel
                    {
                        /// <summary>
                        /// $表注释$
                        /// </summary>
                        [Serializable]
                        [DataContract]
                        public class $表名$
                        {
                            $内容$
                        }
                    }";
        #endregion

        #region V1模板 弃用

        static string mb_V1 = @"using System;
                    using System.Collections.Generic;
                    using System.Linq;
                    using System.Runtime.Serialization;
                    using System.Text;
                    using LambdaToSql;
                    using LambdaToSql.FrameWork;
                    using LambdaToSql.Extended; 
                    
                    namespace EntityModel
                    {

                        /// <summary>
                        /// $表注释$
                        /// </summary>
                        [Serializable]
                        [DataContract]
                        public class $表名$
                        {

                            private EntityBase.RequestEntity Header { get; set; }
                            public $表名$(EntityBase.RequestEntity Header)
                            {
                                this.Header = Header;
                            }

                            public $表名$() { }

                            $内容$

                            public void SetV3Base<T>(T source) where T : class, new()
                            {
                                Tools.SetBase(source, this);

                                this.Guid = System.Guid.NewGuid();
                                this.CreateTime = DateTime.Now;
                                this.IsDelete = (int)Enum.IsDelete.Normal;
                                this.CreateUserInfoID = Header.LoginCache.UserInfoID;
                            }
      
                            public void SetV3BaseUpdate<T>(T source) where T : class, new()
                            {
                                Tools.SetUpdateBase(source, this);
                            }
                        }
                    }";
        #endregion

        #region 属性

        /// <summary>
        /// 上下文对象
        /// </summary>
        public LambdaToSql.EntityModel.DbContext Context { get; set; }

        #endregion

        #region 生成全部实体

        /// <summary>
        /// 生成所有实体
        /// </summary>
        /// <param name="SavedFolder">存储目录 虚拟目录</param>
        /// <param name="mbType">默认 不填</param>
        public void Create(string SavedFolder, string mbType = "mb_basic")
        {
            var list = this.Context.Table;
            foreach (var item in list)
            {
                CreateByTableName(SavedFolder, item.Key, mbType);
            }
        }

        /// <summary>
        /// 生成指定表集合的实体
        /// </summary>
        /// <param name="SavedFolder">存储目录 虚拟目录</param>
        /// <param name="tableList">表集合</param>
        /// <param name="mbType">默认 不填</param>
        public void CreateByTable(string SavedFolder, List<string> tableList, string mbType = "mb_basic")
        {
            var list = this.Context.Table.Where(ex => tableList.Contains(ex.Key)).ToList();
            foreach (var item in list)
            {
                CreateByTableName(SavedFolder, item.Key, mbType);
            }
        }

        /// <summary>
        /// 生成单表实体
        /// </summary>
        /// <param name="SavedFolder">存储目录 虚拟目录</param>
        /// <param name="tableName">表名称</param>
        /// <param name="mbType">默认 不填</param>
        private void CreateByTableName(string SavedFolder, string tableName, string mbType = "mb_basic")
        {
            var item = this.Context.Table.Where(ex => ex.Key == tableName).FirstOrDefault();

            var table = item.Value;
            if (SavedFolder.Substring(SavedFolder.Length - 1, 1) != "\\")
            {
                SavedFolder += "\\";
            }
            if (!Directory.Exists(SavedFolder))
            {
                Directory.CreateDirectory(SavedFolder);
            }

            string writePath = SavedFolder + table.Name + ".cs";
            string str = mb_basic;
            if (mbType == "V1")
            {
                str = mb_V1;
            }

            str = str.Replace("$表名$", table.Name);
            str = str.Replace("$表注释$", table.Comments);

            var content = "";
            foreach (var col in table.Columns)
            {
                var type = Tools.GetType(col.Type);
                if (col.Name == "Guid")
                {
                    type = "System.Guid";
                }
                content += string.Format(@"/// <summary>
                                        /// {0}
                                        /// </summary>
                                        [DataMember]
                                        public {1} {2} {{ get; set; }}", col.Comments, type, col.Name);
                content += "\r\n\r\n";

            }
            str = str.Replace("$内容$", content);
            Tools.FileWrite(writePath, str);
        }
        #endregion
    }
}
